package com.thunder.reader.ui;

import java.util.HashMap;

import com.thunder.reader.R;
import com.thunder.reader.provider.Feed;

import android.app.Activity;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class DeleteItemActivity extends Activity {
    public static final String EXTRA_GROUP_ID = "extra_item_id";
    public static final String EXTRA_SOURCE_ID = "extra_source_id";
    public static final String EXTRA_ARTICLE_ID = "extra_article_id";
    
    private static final ItemData GROUP_ITEM = 
        new ItemData(Feed.Group.CONTENT_URI, R.string.delete_group, R.string.delete_group_prompt);
    private static final ItemData FEED_ITEM = 
        new ItemData(Feed.Source.CONTENT_URI, R.string.delete_feed, R.string.delete_feed_prompt);
    private static final ItemData ARTICLE_ITEM = 
        new ItemData(Feed.Article.CONTENT_URI, R.string.delete_article, R.string.delete_article_prompt);
    private static final HashMap<String, ItemData> ITEM_MAP = new HashMap<String, ItemData>();
    static {
        ITEM_MAP.put(EXTRA_GROUP_ID, GROUP_ITEM);
        ITEM_MAP.put(EXTRA_SOURCE_ID, FEED_ITEM);
        ITEM_MAP.put(EXTRA_ARTICLE_ID, ARTICLE_ITEM);
    }
    
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.delete_item_activity);
        final Intent intent = getIntent();
        String extraName = null;
        if (intent == null) {
            // return or finish?
            return;
        }
        // TODO: is there a better way to know this extra name?
        if (intent.hasExtra(EXTRA_GROUP_ID)) {
            extraName = EXTRA_GROUP_ID;
        } else if (intent.hasExtra(EXTRA_SOURCE_ID)) {
            extraName = EXTRA_SOURCE_ID;
        } else if (intent.hasExtra(EXTRA_ARTICLE_ID)) {
            extraName = EXTRA_ARTICLE_ID;
        }
        if (TextUtils.isEmpty(extraName)) {
            return;
        }
        int itemId = -1;
        itemId = (int) intent.getLongExtra(extraName, -1);
        if (itemId == -1) {
            // TODO: return or finish()?
            return;
        }
        // We need to know this is to delete what?
        // in detail, we need to know: uri, title, and prompt message
        final ItemData item = ITEM_MAP.get(extraName);
        final Uri itemUri = ContentUris.withAppendedId(item.mItemUri, itemId);
        final TextView dialogTitle = (TextView) findViewById(R.id.delete_item_title);
        dialogTitle.setText(item.mDialogTitleResId);
        final TextView message = (TextView) findViewById(R.id.delete_item_message);
        message.setText(getString(item.mDialogMessageResId, getItemTitle(itemUri)));
        final Button buttonDelete = (Button) findViewById(R.id.delete_item_delete);
        final Button buttonCancel = (Button) findViewById(R.id.delete_item_cancel);
        buttonCancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });
        buttonDelete.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                getContentResolver().delete(itemUri, null, null);
                finish();
            }
        });
    }
    
    private String getItemTitle(Uri itemUri) {
        final Cursor c = getContentResolver().query(itemUri, new String[]{Feed.Group.TITLE}, null, null, null);
        if (c != null && c.moveToFirst()) {
            return c.getString(0);
        }
        return "";
    }
    
    private static class ItemData {
        Uri mItemUri;
        int mDialogTitleResId;
        int mDialogMessageResId;
        
        ItemData(Uri uri, int title, int message) {
            mItemUri = uri;
            mDialogTitleResId = title;
            mDialogMessageResId = message;
        }
    }
}
